package com.example.shoppingmallclient.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shoppingmallclient.R;
import com.example.shoppingmallclient.ServerIp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

@SuppressWarnings("all")
public class Login extends AppCompatActivity {

    private Button btnLogin;
    private EditText evUserNumber;
    private EditText evPassword;
    private TextView gotoRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        findViews();
        setListeners();
    }

    private void findViews(){
        btnLogin = findViewById(R.id.btn_login);
        evUserNumber = findViewById(R.id.usernumber);
        evPassword = findViewById(R.id.password);
        gotoRegister = findViewById(R.id.goto_register);
    }

    private void setListeners(){
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new login().execute();
            }
        });

        gotoRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(Login.this, Register.class);
                startActivity(intent);
            }
        });
    }

    private class login extends AsyncTask<Object, Object, String>{
        @Override
        protected String doInBackground(Object[] objects) {
            try {
                String serverUrl = "http://" + ServerIp.ServerIp + ":8080/LoginServlet";
                URL url = new URL(serverUrl);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");


                //获取用户名和密码的字符串
                String userNumber = evUserNumber.getText().toString();
                String password = evPassword.getText().toString();
                System.out.println(userNumber);
                String msg = userNumber + "," + password;
                System.out.println(msg);
                OutputStream os = connection.getOutputStream();
                os.write(msg.getBytes());
                os.flush(); //刷新缓存

                //接收服务器发来的消息
                InputStream is = connection.getInputStream();
                StringBuffer stringBuffer = new StringBuffer();
                int len = 0;
                byte[] bytes = new byte[1024];
                while ((len = is.read(bytes)) != -1){
                    stringBuffer.append(new String(bytes, 0, len));
                }

                String result = stringBuffer.toString();
                return result;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            if("true".equals(result)){
                Intent intent = new Intent();
                intent.setClass(Login.this, TabHostActivity.class);
                startActivity(intent);
            }else {
                Toast.makeText(Login.this, "请输入正确的账号和密码", Toast.LENGTH_SHORT).show();
            }
        }
    }
}